// run: $exec < input
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<algorithm>

const int maxn = 200000+100;
char ch[4];

struct SuffixArray
{
	char s[maxn];
	int sa[maxn],rank[maxn],height[maxn];
	int t1[maxn],t2[maxn],c[maxn],n;
	void build_sa(int m)
	{
		int i,*x=t1,*y=t2;
		for(i=0;i<m;i++) c[i]=0;
		for(i=0;i<n;i++) c[x[i]=s[i]]++;
		for(i=1;i<m;i++) c[i]+=c[i-1];
		for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
		for(int k=1;k<=n;k<<=1)
		{
			int p=0;
			for(i=n-k;i<n;i++) y[p++]=i;
			for(i=0;i<n;i++)if(sa[i]>=k) y[p++]=sa[i]-k;
			for(i=0;i<m;i++) c[i]=0;
			for(i=0;i<n;i++) c[x[y[i]]]++;
			for(i=1;i<m;i++) c[i]+=c[i-1];
			for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
			std::swap(x,y);
			p=1;x[sa[0]]=0;
			for(i=1;i<n;i++)
				x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
			if(p>=n) break;
			m=p;
		}
	}
	void build_height()
	{
		int i,j,k=0;
		for(i=0;i<n;i++) rank[sa[i]]=i;
		for(i=0;i<n;i++)
		{
			if(k) k--;
			j=sa[rank[i]-1];
			while(s[i+k] == s[j+k])k++;
			height[rank[i]]=k;
		}
	}
} sa;

int right[maxn];

long long solve()
{
	long long ans=0;
	right[sa.n] = sa.n;
	for (int i = sa.n - 1; i >= 1; i--) {
		if (sa.s[i - 1] == ch[0]) right[i] = i;
		else right[i] = right[i + 1];
	}
	for(int i=1;i<sa.n;i++) {
		long long tmp = sa.n - 1 - sa.sa[i] - sa.height[i];
//		std::printf("%d %d %d %lld\n", sa.sa[i], sa.height[i], right[sa.sa[i] + 1], tmp);
		if (right[sa.sa[i] + 1] <= sa.sa[i] + 1 + sa.height[i]) {
			ans += tmp;
		} else {
			tmp = std::max(0ll, tmp - (right[sa.sa[i] + 1] - sa.sa[i] - sa.height[i] - 1));
			ans += tmp;
		}
//		std::printf("%lld\n", ans);
	}
	return ans;
}

int main()
{
	int T; scanf("%d",&T);
	for (int ti = 1; ti <= T; ti++) {
		printf("Case #%d: ", ti);
		scanf("%s%s", ch, sa.s);
		sa.n=strlen(sa.s)+1;
		sa.s[sa.n-1]=0;
		sa.build_sa(300);
		sa.build_height();
		printf("%lld\n", solve());
	}
}

